<?php

function readTxt($txt, $version, $os) {
    $resultArray = array();
    $file = "../game/" . $txt;
    if (file_exists($file)) {
        $f = fopen($file, "r");
        while (!feof($f)) {
            $line = fgetss($f);
            $array = explode("=", $line);
            if ($array[0] === $version && $array[1] === $os) {
                $url = trim($array[3]);
                if (substr($url, -4) == "\r\n") {
                    $url = substr($url, strlen($url) - 4);
                }
                if ($url !== "") {
                    $resultArray[$array[2]] = $url;
                }
            }
        }
        fclose($f);
    }
    return $resultArray;
}

function getAllTitlePacketByOSVersion(DBC $db, $version, $os) {
    $sql = "SELECT name,price,goodprice,promotion,iconUrl,newIconUrl,qstUrl,musicUrl,imageUrl,version,iconVersion,gold,dollar,title,dollarID FROM t_hibaby_title_packet WHERE os = '" . $os . "' AND version = '".$version."' ORDER BY name";
    $titlePacketArray = $db->fetchData($sql);
    return fillData($titlePacketArray);
}

function fillData($data) {
    if (checkArrayNotEmpty($data)) {
        for ($i = 0; $i < count($data); $i++) {
            if (isNull($data[$i]['gold'])) {
                $data[$i]['gold'] = "0";
            }
            if (isNull($data[$i]['dollar'])) {
                $data[$i]['dollar'] = "0";
            }
            if (isNull($data[$i]['dollarID'])) {
                $data[$i]['dollarID'] = "0";
            }
            if (isNull($data[$i]['title'])) {
                $data[$i]['title'] = "";
            }
            if (isNull($data[$i]['iconUrl'])) {
                $data[$i]['iconUrl'] = "";
            }
            if (isNull($data[$i]['newIconUrl'])) {
                $data[$i]['newIconUrl'] = "";
            }
            if (isNull($data[$i]['qstUrl'])) {
                $data[$i]['qstUrl'] = "";
            }
            if (isNull($data[$i]['musicUrl'])) {
                $data[$i]['musicUrl'] = "";
            }
            if (isNull($data[$i]['imageUrl'])) {
                $data[$i]['imageUrl'] = "";
            }
            if (isNull($data[$i]['iconVersion'])) {
                $data[$i]['iconVersion'] = "0";
            }
        }
    }
    return $data;
}

?>
